package models

import (
	"gorm.io/gorm"
	"tentx-srv/basic/config"
	__ "tentx-srv/basic/proto"
)

type Video struct {
	gorm.Model
	Title     string `gorm:"type:varchar(30);not null;comment:'视频标题'"`
	VideoUrl  string `gorm:"type:varchar(200);not null;comment:'视频的地址'"`
	ConUrl    string `gorm:"type:varchar(200);not null;comment:'视频的封面'"`
	Status    int    `gorm:"type:int;not null;comment:'视频状态 0 已下架 1 未下架'"`
	Watchtime int    `gorm:"type:int;not null;comment:'观看时长'"`
	UserId    int    `gorm:"type:int;not null;comment:'用户id'"`
}

type Member struct {
	gorm.Model

	Price   float64 `gorm:"type:decimal(10,4);not null;comment:'会员价格'"`
	Status  int     `gorm:"type:int;not null;comment:'会员类型 0 vip 1 svip'"`
	VideoId int     `gorm:"type:int;not null;comment:'用户id'"`
	UserId  int     `gorm:"type:int;not null;comment:'用户id'"`
}

func (m *Member) MemberCreate(db *gorm.DB) error {
	err := db.Create(&m).Error
	return err
}

func (m *Member) MemberFind() (list []*__.MemberList, err error) {
	err = config.DB.Model(Member{}).Limit(1).Find(list).Error
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

func (v *Video) VideoFind(db *gorm.DB, page, pageSize int) (list []*__.TentList, err error) {
	err = db.Model(Video{}).Scopes(Paginate(page, pageSize)).Offset(page).Limit(pageSize).Limit(1).Find(list).Error
	return
}

func (v *Video) FindVideo(db *gorm.DB) (list []*__.TentList, err error) {
	err = db.Model(Video{}).Limit(1).Find(list).Error
	return
}

func (v *Video) CreateVideo(db *gorm.DB) error {
	err := db.Create(&v).Error
	return err
}
